package userInterface;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.beans.PropertyChangeEvent;

import javax.swing.JFrame;
import adaptationLayer.AdaptersFactory;
import contextManagement.ContextBroker;
import java.awt.Dimension;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JPanel;
//import javax.swing.border.TitledBorder;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

public class GraphicalUserInterface extends JFrame implements org.osgi.framework.BundleActivator,ItemListener{
	private static final long serialVersionUID = 1L;
	protected UserInterfaceStrategy myPanelContent=null;
	protected DataModel myModel;
	private static BundleContext bundlecontext;
        GraphicalUserInterface gui;
        private JFrame jf;
	private JPanel jpHaut;
	private JComboBox jc;
	private JPanel jpBas;
        private static ContextBroker cb;

     /*
	public GraphicalUserInterface(String panelContentClass){
		AdaptersFactory.createAdapters(this);
		setUserInterfaceStrategy(panelContentClass);
	}
	*/
/*
	public void setUserInterfaceStrategy (String panelContentClass){
		Container pan=this.getContentPane();
		pan.removeAll();
		if (panelContentClass.equals("BarUserInterfaceStrategy")){
			myPanelContent=new BarUserInterfaceStrategy(myModel);
		} else{
			myPanelContent=new TableUserInterfaceStrategy(myModel);
		}
		pan.setLayout(new BorderLayout());
		pan.add (myPanelContent.getComponent(), BorderLayout.CENTER);
		if (isVisible()==true){
			this.setVisible(false);
			this.setVisible(true);
		}
	}
*/

        public void setUserInterfaceStrategy(String strategyName){
		if(strategyName.equals("TableUserInterface")){
			myPanelContent = new TableUserInterfaceStrategy(myModel);
		} else{
			myPanelContent = new BarUserInterfaceStrategy(myModel);
			
		}
                jpBas.add(myPanelContent.getComponent(), BorderLayout.CENTER);
		jf.pack();
	}
	/*public UserInterfaceStrategy getUserInterfaceStrategy(){
		return this.myPanelContent;
		
	}
	*/
	
	/*public DataModel getDataModel(){
		return myModel;
	}
	*/
	
	public void addData(Object[] newData){
		myModel.appendValue(newData);
	}

	/**
	 * @param args
	 */
	/*public static void main(String[] args) {
		//testTableUserInterfaceStrategy(args);
		//testGraphicsUserInterfaceStrategy(args);
		//testSwitchUserInterface(args);
		start();
	}
	*/
	public void start(org.osgi.framework.BundleContext bc){
		//GraphicalUserInterface ui=new GraphicalUserInterface();
                bundlecontext=bc;
		this.setTitle("TableUserInterfaceStrategy");
		this.setSize(300,300);
		this.setVisible(true);
                String[] args = null;
            //    testTableUserInterfaceStrategy(args);
           //     testBarUserInterfaceStrategy(args);
          //      testSwitchUserInterface(args);
                gui = new GraphicalUserInterface();
                gui.graphicalUserInterfaceInit();
		
	}
	public void stop(org.osgi.framework.BundleContext bc){
            ContextBroker myBroker=ContextBroker.getInstance();
            PropertyChangeEvent e=new PropertyChangeEvent(this, "mort",new Integer(-1), new Integer(0));
	    myBroker.firePropertyChange(e);
            gui.jf.dispose();
            gui.dispose();
	}

        public static BundleContext getBundleContext(){
            BundleContext bc = bundlecontext;
            return bc;
        }

        /*
	public static void testSwitchUserInterface (String[] args){
		GraphicalUserInterface ui=new GraphicalUserInterface("BarUserInterfaceStrategy");
		ui.setSize(300,300);
		ui.setVisible(true);
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		ui.setUserInterfaceStrategy("TableUserInterfaceStrategy");
	}
	public static void testBarUserInterfaceStrategy(String[] args){
		DataModel model=new DataModel();
		GraphicalUserInterface ui=new GraphicalUserInterface("BarUserInterfaceStrategy");
		ui.setSize(300,300);
		ui.setVisible(true);
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Object[][] data = {
			    {"date1", "11"},
			    {"date2", "21"},
			    {"date3", "31"},
			    {"date4", "41"},
				   
			};
		model.setValues(data);
	}
	
	public static void testTableUserInterfaceStrategy(String[] args){		
		DataModel model=new DataModel();
		GraphicalUserInterface ui=new GraphicalUserInterface("TableUserInterfaceStrategy");
		ui.setSize(300,300);
		ui.setVisible(true);
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Object[][] data = {
			    {"date1", "11"},
			    {"date2", "21"},
			    {"date3", "31"},
			    {"date4", "41"},
				   
			};
		model.setValues(data);
		
	}
	*/
        public void graphicalUserInterfaceInit(){
            AdaptersFactory.createAdapters(this);
            GraphicalUserInterface.cb = ContextBroker.getInstance();
            jf = new JFrame("Frigo Application");
            jf.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) 
			{
				try {
					GraphicalUserInterface.bundlecontext.getBundle().stop();
				} catch (BundleException ex) {
					ex.printStackTrace();
				}
			}
            });
               
            myModel = new DataModel();
            jpHaut = new JPanel();

            String labels[] = {"TableUserInterface", "BarUserInterface"};
            jc = new JComboBox(labels);
            jc.setEditable(false);
            jc.setPreferredSize(new Dimension(200,20));
            jpHaut.add(jc, BorderLayout.LINE_END);
            jc.addItemListener(this);
		
            jpBas = new JPanel();
		
            jf.getContentPane().add(jpHaut, BorderLayout.NORTH);
            jf.getContentPane().add(jpBas, BorderLayout.CENTER);
		
            String strategy = "TableUserInterface";
            setUserInterfaceStrategy(strategy);

            jf.pack();
            jf.setLocationRelativeTo(null);
            jf.setVisible(true);
//            cb.addPropertyChangeListener("uPref",this);
	}

    public void itemStateChanged(ItemEvent ie) {
        String item = (String)ie.getItem();
        cb.changeUPref(item);
	cb.firePropertyChange(new PropertyChangeEvent(this, "chItem", Boolean.valueOf(!true), Boolean.valueOf(true)));
        jpBas.removeAll();
        setUserInterfaceStrategy(item);
    }
/*
    public void propertyChange(PropertyChangeEvent pce) {
        if(pce.getPropertyName().equals("uPref")) {
            String strategy = (String) pce.getNewValue();
            jpBas.removeAll();
            setUserInterfaceStrategy(strategy);
        }
 
 */
    }



